package com.ansion.utils.bus

import com.ansion.utils.bus.BaseCoroutine.mScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.CoroutineContext

object BaseCoroutine {
    var mScope: CoroutineScope? = null
}

internal class BackgroundScope(context: CoroutineContext) : CoroutineScope {
    override val coroutineContext: CoroutineContext = context
    override fun toString(): String = "CoroutineScope(coroutineContext=$coroutineContext)"
}

@Suppress("FunctionName")
fun DefaultBGScope(): CoroutineScope = BackgroundScope(SupervisorJob() + Dispatchers.Default)

@Suppress("FunctionName")
fun BGScope(): CoroutineScope {
    if (mScope == null) {
        mScope = DefaultBGScope()
    }
    return mScope!!
}
